﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormBase
{
    public partial class FPGM0020 : Form
    {
        //CProject mProject = Program.gProject;
        string msScanned = string.Empty;
        string msBarcode = string.Empty;
        bool mbBarcodeShowing = false;

        public FPGM0020()
        {
            InitializeComponent();
        }

        private void FPGM0020_Load(object sender, EventArgs e)
        {
            Text = "Barcode scanned by keyboard";
            KeyPreview = true;
            myClear();
        }

        private void myClear()
        {
            txtBarcode.Clear();
            txtMsg.Clear();
            txtHex.Clear();
            txtLength.Clear();
            ActiveControl = txtBarcode;
        }
        private void myClose()
        {
            Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            myClear();
        }

        private void FPGM0020_FormClosing(object sender, FormClosingEventArgs e)
        {
            // release any open resources.
        }

        private void FPGM0020_KeyPress(object sender, KeyPressEventArgs e)
        {
            string s1 = string.Empty;
            char cInputKeyPress = e.KeyChar;
            switch (cInputKeyPress)
            {
                case (char)Keys.Escape:
                    DialogResult d1 = MessageBox.Show("Close ?" + s1, "Confirm", MessageBoxButtons.OKCancel);
                    if (d1 == DialogResult.OK)
                    {
                        //CProject.End();
                        this.Close();
                    }
                    break;
                //case (char)Keys.Enter:
                //case CProject.CR: // CR=Keys.Enter, LF = Keys.Return
                //case CProject.LF: // CR=Keys.Enter, LF = Keys.LineFeed
                case (char) Keys.LineFeed:
                    CProject.mApp.mAction.AppendMsgDebug("FPGM0020.KeyPress().Enter");
                    msScanned = msScanned + ((char)Keys.LineFeed);
                    CProject.mApp.mAction.AppendMsgDebug("msBarcodeScanned=" + msScanned);
                    Boolean bFound = false;
                    //txtHex.Text = mProject.StringASCToHex(msScanned);
                    msBarcode = myGetBarcode(msScanned);
                    if (msBarcode.Length < 1)
                    {
                        e.Handled = true;
                        return;
                    }
                    //txtBarcode.Text = msBarcode;
                    switch (msBarcode)
                    {
                        case "T": // Test
                            bFound = true;
                            msBarcode = "Test";
                            break;
                        case "V": // Version
                            bFound = true;
                            msBarcode = CProject.mApp.msAssemblyVersion;
                            break;
                        case "7192751043689":
                            bFound = true;
                            break;
                        case "ABCDEFGH":
                            bFound = true;
                            break;
                    }
                    if (bFound)
                        myShowFound();
                    else
                        myShowNotFound();

                    msScanned = string.Empty;
                    //e.Handled = true;
                    break;

                case (char)Keys.F1:
                    // KeyPrress抓不到F1, 改用輸入VERSION字串處理.
                    //MessageBox.Show(String.Format("Version {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString()), "Information");
                    CProject.mApp.PopupMsg(CProject.mApp.msAssemblyVersion);
                    //e.Handled = true;
                    break;
                default:
                    msScanned += cInputKeyPress;
                    //e.Handled = true;
                    break;
            }
            e.Handled = true;
        }
        private void myShowFound()
        {
            mbBarcodeShowing = true;
            myClear();
            txtBarcode.Text = msBarcode;
            txtLength.Text = msScanned.Length.ToString();
            txtHex.Text = CProject.gStringASCToHex(msScanned);
            txtMsg.Text = "Success.";
        }
        private void myShowNotFound()
        {
            mbBarcodeShowing = true;
            myClear();
            txtBarcode.Text = msBarcode;
            txtLength.Text = msScanned.Length.ToString();
            txtHex.Text = CProject.gStringASCToHex(msScanned);
            txtMsg.Text = "Success scanned but not found in DB.";
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private string myGetBarcode(string sInput)
        {
            string s1;

            s1 = CProject.gCRLFRemove(sInput);
            return s1;

        }
    }
}
